define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme','form', 'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Form,Template) {

    var Controller = {
        index: function () {
            // 基于准备好的dom，初始化echarts实例
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');

            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: '',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['众包费', '任务数']
                },
                toolbox: {
                    show: false,
                    feature: {
                        magicType: {show: true, type: ['stack', 'tiled']},
                        saveAsImage: {show: true}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: Orderdata.column
                },
                yAxis: {},
                grid: [{
                    left: 'left',
                    top: 'top',
                    right: '10',
                    bottom: 30
                }],
                series:[{
                    name: '众包费',
                    type: 'line',
                    smooth: true,
                    areaStyle: {
                        normal: {}
                    },
                    lineStyle: {
                        normal: {
                            width: 1.5
                        }
                    },
                    data: Orderdata.moneydata
                },
                    {
                        name: '任务数',
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: Orderdata.taskdata
                    }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);

            $(window).resize(function () {
                myChart.resize();
            });

            $(document).on("click", ".btn-checkversion", function () {
                top.window.$("[data-toggle=checkupdate]").trigger("click");
            });

        },

        taskmoneycompany: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'statistics/taskmoneycompany' + location.search,
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                searchFormVisible: true,
                showExport: false,
                columns: [
                    [
                        {field: 'id', title: '众包费id', operate:false},
                        {field: 'money', title:'众包费金额', operate:false},
                        {field: 'ca_money', title: '应税所得额', operate:false},
                        {field: 'get_money', title:'所得金钱', operate:false},
                        {field: 'createtime', title: '结算时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field:'name', title:'任务名称', operate:false},
                        {field: 'nickname', title:'用户', operate:false}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            var myChart2 = Echarts.init(document.getElementById('echart'), 'walden');

            // 指定图表的配置项和数据
            var option = {
                title: {
                    text: '',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['众包费', '任务数']
                },
                toolbox: {
                    show: false,
                    feature: {
                        magicType: {show: true, type: ['stack', 'tiled']},
                        saveAsImage: {show: true}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: Orderdata2.column
                },
                yAxis: {},
                grid: [{
                    left: 'left',
                    top: 'top',
                    right: '10',
                    bottom: 30
                }],
                series: [{
                    name: '众包费',
                    type: 'line',
                    smooth: true,
                    areaStyle: {
                        normal: {}
                    },
                    lineStyle: {
                        normal: {
                            width: 1.5
                        }
                    },
                    data: Orderdata2.moneydata
                },
                {
                    name: '任务数',
                        type: 'line',
                    smooth: true,
                    areaStyle: {
                    normal: {}
                },
                    lineStyle: {
                        normal: {
                            width: 1.5
                        }
                    },
                    data: Orderdata2.taskdata
                }
                ]
            };

            // 使用刚指定的配置项和数据显示图表。
            myChart2.setOption(option);
            $(window).resize(function () {
                myChart2.resize();
            });
        },

        index_company: function () {
            Controller.api.bindevent();
            Form.api.bindevent($('.panel-body'),function () {

            });

            var url = 'statistics/index_company';
            //绑定事件
            $('a[data-toggle="tab"]').on('click', function (e) {

                var time_type = $(this).data('value');
                if(time_type == 6){
                    $("#create_div").removeClass('hidden');
                    return;
                }else{
                    if(!$("#create_div").hasClass('hidden')){
                        $("#create_div").addClass('hidden');
                    }
                }
                $("#createtime").val('');
                getCompanyInfo(time_type);
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            $("#createtime_button").on('click', function(){
                getCompanyInfo(6);
            })
            function getCompanyInfo(time_type){
                var time_data = $("#createtime").val();
                var type_id = $("#type_id").val();
                Fast.api.ajax({
                    url: url,
                    data: {
                        time_type: time_type,
                        time_data : time_data,
                        type_id : type_id,
                    }
                }, function (ret) {
                    $("#companyTaskAmount").html(ret.companyTaskAmount);
                    $("#tax_money").html(ret.tax_money);
                    $("#already_money").html(ret.already_money);
                    $("#totalTask").html(ret.totalTask);

                    Orderdata3 = {
                        column: ret.column,
                        moneydata: ret.moneydata,
                        taskdata:ret.taskdata,
                    };

                    getchartdata();
                    return false;
                });
            }

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
            var myChart3 = Echarts.init(document.getElementById('echart'), 'walden');
            // 基于准备好的dom，初始化echarts实例
            function getchartdata(){

                // 指定图表的配置项和数据
                var option = {
                    title: {
                        text: '',
                        subtext: ''
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: [ '众包费', '任务数']
                    },
                    toolbox: {
                        show: false,
                        feature: {
                            magicType: {show: true, type: ['stack', 'tiled']},
                            saveAsImage: {show: true}
                        }
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: Orderdata3.column
                    },
                    yAxis: {},
                    grid: [{
                        left: 'left',
                        top: 'top',
                        right: '10',
                        bottom: 30
                    }],
                    series: [{
                        name: '众包费',
                        type: 'line',
                        smooth: true,
                        areaStyle: {
                            normal: {}
                        },
                        lineStyle: {
                            normal: {
                                width: 1.5
                            }
                        },
                        data: Orderdata3.moneydata
                    },
                        {
                            name: '任务数',
                            type: 'line',
                            smooth: true,
                            areaStyle: {
                                normal: {}
                            },
                            lineStyle: {
                                normal: {
                                    width: 1.5
                                }
                            },
                            data: Orderdata3.taskdata
                        }
                    ]
                };

                // 使用刚指定的配置项和数据显示图表。
                myChart3.setOption(option);
            }

            $(window).resize(function () {
                myChart3.resize();
            });

            $(document).on("click", ".btn-checkversion", function () {
                top.window.$("[data-toggle=checkupdate]").trigger("click");
            });
            getchartdata();
        },

        index_dealer: function () {
            Controller.api.bindevent();
            Form.api.bindevent($('.panel-body'),function () {

            });
            var url = 'statistics/index_dealer';
            //绑定事件
            $('a[data-toggle="tab"]').on('click', function (e) {

                var time_type = $(this).data('value');
                if(time_type == 6){
                    $("#create_div").removeClass('hidden');
                    return;
                }else{
                    if(!$("#create_div").hasClass('hidden')){
                        $("#create_div").addClass('hidden');
                    }
                }
                $("#createtime").val('');
                getDealerInfo(time_type);
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            $("#createtime_button").on('click', function(){
                getDealerInfo(6);
            })
            function getDealerInfo(time_type){
                var time_data = $("#createtime").val();
                Fast.api.ajax({
                    url: url,
                    data: {
                        time_type: time_type,
                        time_data : time_data,
                    }
                }, function (ret) {
                    $("#withdraw_amount").html(ret.withdraw_amount);
                    $("#dealer_task_amount").html(ret.dealer_task_amount);
                    $("#company_num").html(ret.company_num);
                    $("#dealer_amount").html(ret.dealer_amount);
                    $("#dealer_usermoney").html(ret.dealer_usermoney);
                    $("#table").html("");
                    var table_info = "" ;
                    $.each(ret.dealer_company_list, function(i, v){
                        table_info += "<tr>";
                        table_info += "<td>" + v.name + "</td>";
                        table_info += "<td>" + v.rate + "</td>";
                        table_info += "<td>" + v.concat_phone + "</td>";
                        table_info += "<td>" + v.code + "</td>";
                        table_info += "<td>" + v.createtime + "</td>";
                        table_info += "</tr>";
                    })
                    $("#table").html(table_info);
                    return false;
                });
            }
        },

        api: {
            bindevent: function () {

            }
        }
    };

    return Controller;
});
